function TagMenu() {
    this.tagMenuItems = [];
    this.menuListElem = $('<ul/>');
    this.menuListElem.addClass("menu");
    addToBody(this.menuListElem);
    this.currentTag = null;
    this.onTagSelectedEventHandler = function() { };
}

TagMenu.prototype.load = function () {
    var me = this;
    loadAllTagsAnd(function(tags) {
        me.handleLoadAllTags(tags);
    });
};

TagMenu.prototype.onTagSelected = function (eventHandler) {
    this.onTagSelectedEventHandler = eventHandler;
};

TagMenu.prototype.handleLoadAllTags = function (tags) {
    var me = this;
    tags.forEach(function (tag) {
        me.addTag(tag, function () {
            me.tagWasSelected(tag);
        });
    });

    this.addTag("about", function () {
        me.onTagSelectedEventHandler("about");
    });
};

TagMenu.prototype.setCurrentTag = function (tag) {
    this.currentTag = tag;
};

TagMenu.prototype.tagWasSelected = function (tag) {
    this.setCurrentTag(tag);
    this.onTagSelectedEventHandler(tag);
};

TagMenu.prototype.addTag = function (tagName, callback) {
    var tagMenuItem = new TagMenuItem(tagName, this, callback);
    if (this.tagMenuItems.length > 0) {
        tagMenuItem.previous = this.tagMenuItems.peek();
        tagMenuItem.previous.next = tagMenuItem;
    }
    this.tagMenuItems.push(tagMenuItem);
};

function TagMenuItem(name, menu, callback) {
    this.name = name;
    this.menuItem = $("<li/>");
    this.linkUI = $("<a/>");
    this.linkUI.addClass("tagLink main");
    this.linkUI.attr("id", "tag" + name);
    this.linkUI.text(name.toUpperCase());

    var thisTag = this;

    this.linkUI.click(function () {
        callback();
        thisTag.active();
    });

    this.menuItem.append(this.linkUI);
    menu.menuListElem.append(this.menuItem);
};

TagMenuItem.prototype.active = function () {
    this.menuItem.addClass("active-menu-item");
    var linkedListHelper = new LinkedListHelper(this);
    var thisTag = this;
    linkedListHelper.forEach(function (tag) {
        if (tag != thisTag) {
            tag.inactive();
        }
    });
};

TagMenuItem.prototype.inactive = function () {
    this.menuItem.removeClass("active-menu-item");
};

